package com.zl.ots.utils;

import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateUtils{

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 时间字符串转换成LocalDateTime
     */
    public static LocalDateTime parseDateTime(String date, String pattern) {
        if (!ObjectUtils.isEmpty(pattern)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(date, formatter);
        }
        return LocalDateTime.parse(date, DATE_TIME_FORMATTER);
    }

    /**
     * 时间字符串转换成LocalDateTime
     */
    public static LocalDate parseDate(CharSequence text) {
        if (null == text) {
            return null;
        }
        return LocalDate.parse(text);
    }
}